package com.yzg.leetcode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {

    public static void main(String[] args) {

//        ListNode l1 = new ListNode();
//        l1.val=1;
//        l1.next.val=2;
//        l1 = new ListNode(l1, l1.next.val);

//        输入：l1 = [2,4,3], l2 = [5,6,4] 输出：[7,0,8] 解释：342 + 465 = 807.
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ret = new ListNode();
        int carry = 0;//进位
        while (l1 != null || l2 != null) {
            int l1Val = l1 == null ? 0 : l1.val;
            int l2val = l2 == null ? 0 : l2.val;

            int sum = l1Val + l2val + carry;
            int j = sum % 10;//个位数
            ret = new ListNode(j);
            carry = sum / 10;//十位数
        }

        return ret;
    }


    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}

